package cn.airfei.netty.nio;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @description:
 * @author: air
 * @create: 2020-12-31 11:22
 */
public class MappedByteBufferTest {
    public static void main(String[] args) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("1.txt", "rw");

        FileChannel randomAccessFileChannel = randomAccessFile.getChannel();

        /**
         * 参数1:表示使用的是读写模式
         * 参数2:可以直接修改的起始位置
         * 参数3:映射到的内存大小(字节)
         */
        //  MappedByteBuffer 可以实现在内存中读写
        MappedByteBuffer mappedByteBuffer = randomAccessFileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 5);
        mappedByteBuffer.put(0, Byte.parseByte("1"));
        mappedByteBuffer.put((byte) 4);

        randomAccessFile.close();


    }

}
